package com.conv.shop.business.controller;

import com.conv.shop.business.domain.ShoppingCart;
import com.conv.shop.business.req.CartUpdateReq;
import com.conv.shop.business.req.CartInfo;
import com.conv.shop.business.resp.CartResp;
import com.conv.shop.business.service.CartService;
import com.conv.shop.business.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/cart")
public class ShoppingCartController {
    @Resource
    private CartService cartService;

    /**
     * 查询购物车列表
     * @param request
     * @return List<CartResp>
     */
    @GetMapping("/list")
    public List<CartResp> getCartListId(HttpServletRequest request) {
        String token = request.getHeader("token");
        int consumerId = Integer.parseInt(JwtUtil.getUserNameByToken(token));
        return cartService.getCartList(consumerId);
    }

    /**
     * 添加至购物车
     * @param cartInfo
     * @return boolean
     */
    @PostMapping("/add")
    public ResponseEntity<String> CartAdd(@Validated @RequestBody CartInfo cartInfo) {
        String token = cartInfo.getToken();
        int consumerId = Integer.parseInt(JwtUtil.getUserNameByToken(token));
        cartInfo.setConsumerId(consumerId);
        if(cartService.cartAdd(cartInfo)){
            return ResponseEntity.ok("success");
        }
        return ResponseEntity.badRequest().body("fail");
    }

    /**
     *
     * 购物车删除接口
     * @param id
     * @return boolean
     */
    @GetMapping("/deleted")
    public boolean CartDeleted(String id) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setShoppingCartId(Integer.parseInt(id));
        return cartService.cartDelete(shoppingCart);
    }

    /**
     * 更新购物车接口
     * @param cart
     * @param request
     * @return boolean
     */
    @PostMapping("/update")
    public ResponseEntity<String> CartUpdate(@Validated @RequestBody CartUpdateReq cart, HttpServletRequest request) {
        String token = request.getHeader("token");
        int consumerId = Integer.parseInt(JwtUtil.getUserNameByToken(token));
        cart.setConsumerId(consumerId);
        if(cartService.cartUpdate(cart)){
            return ResponseEntity.ok("success");
        }
        return ResponseEntity.badRequest().body("fail");
    }

    /**
     * 清空购物车接口
     * @param request
     * @return boolean
     */
    @GetMapping("/clear")
    public String CartClear(HttpServletRequest request) {
        String token = request.getHeader("token");
        int consumerId = Integer.parseInt(JwtUtil.getUserNameByToken(token));
        if(cartService.cartClear(consumerId)){
            request.setAttribute("msg","success");
            return "redirect:/";
        }
        request.setAttribute("msg","fail");
        return "redirect:/";
    }
}
